package chapter8;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/*@WebFilter(filterName = "logFilter", urlPatterns = { "/*" })
*/public class LogFilter implements Filter {
	private FilterConfig config;

	// 实现初始化方法
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}

	// 实现过滤方法
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// 获得应用上下文对象
		ServletContext context = config.getServletContext();
		// 返回请求对象
		HttpServletRequest hrequest = (HttpServletRequest) request;
		// 记录开始过滤时间
		long start = System.currentTimeMillis();
		System.out.println("用户地址：" + hrequest.getRemoteAddr());
		System.out.println("请求的资源：" + hrequest.getRequestURI());
		context.log("请求的资源：" + hrequest.getRequestURI());
		context.log("用户地址：" + hrequest.getRemoteAddr());
		// 请求转到下一资源或下一过滤器
		chain.doFilter(request, response);
		// 记录返回到过滤器时间
		long end = System.currentTimeMillis();
		System.out.println("请求的总时间：" + (end - start) + "毫秒");
		context.log("请求的总时间：" + (end - start) + "毫秒");
	}

	// 实现销毁方法
	public void destroy() {
		this.config = null;
	}
}
